package com.hzit.controller.admin;

import com.hzit.entity.LoginUser;
import com.hzit.entity.User;
import com.hzit.service.IUserService;
import com.hzit.service.TokenService;
import com.hzit.utils.JwtUtils;
import com.hzit.utils.R;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者：李嘉浩
 * 时间：2024-08-23-20:44
 * 功能：
 */
@RestController
@RequestMapping("/admin/user")
@Api("用户登录")
public class AdminLoginController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    @ApiOperation("账号密码登录")
    public R login(@RequestBody User user){
        LoginUser loginUser=iUserService.login(user);
        Map<String,Object> map=tokenService.createToken(loginUser);
        return R.ok().data(map);
    }

    @GetMapping("/info")
    @ApiOperation("用户信息")
    public R info(@RequestParam String token){
        Claims claims = JwtUtils.parseToken(token);
        String  username = (String) claims.get("username");

        Map<String,Object> map=new HashMap<>();
        map.put("name",username);
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return R.ok().data(map);
    }
}
